
package org.itboys.mobile.service.mongo.mobile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;

import org.itboys.mobile.entity.mongo.SundayApkInfo;
import org.itboys.mobile.util.apk.ApkUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Maps;

@Service
public class SundayMobileApkInfoService extends BaseMongoService<SundayApkInfo> {


	@Autowired
	private ResourceHolder resourceHolder;
	
	private static final long serialVersionUID = -5806434055381127192L;
	@Resource(name="mobileDs")
	private MongoDataSource sundayDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return sundayDataSource;
	}
	@Override
	protected Class<SundayApkInfo> getEntityClass() {
		return SundayApkInfo.class;
	}


	/**
	 * 按code获取最新的APP
	 * @param versionCode
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public SundayApkInfo getApkInfo(Integer versionCode,Integer type) throws Exception{
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("versionCode >", versionCode);
		param.put("type", type);
		param.put("sort", "-versionCode");
		param.put("isDeleted", 0);
		List<SundayApkInfo> infoes = super.list(param);
		if(ListUtil.isNotNull(infoes)){
			return infoes.get(0);
		}
		return null;
	}

	/**
	 * 获取最新版本的下载地址。安卓和苹果都有效，前提是添加苹果的下载地址
	 * @return
	 */
	public String getLastedDownUrl(Integer type){
		String downUrl=null;
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("type", type);
		param.put("sort", "-versionCode");
		param.put("isDeleted", 0);
		List<SundayApkInfo> infoes = super.list(param);
		if(ListUtil.isNotNull(infoes)){
			downUrl= infoes.get(0).getDownLoadUrl();
		}
		return downUrl;
	}
}

